
package com.op.teachingliteapp.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Toast;

import com.op.teachingliteapp.R;
import com.op.teachingliteapp.fragment.resource.ResouceFragment;

import com.op.teachingliteapp.sender.ui.DeviceScanDialog;

/*
 * 这是第二个界面的主Activity
 */
public class CourseActivity extends Activity {
	private static int backChoice = 0;

	public static int getBackChoice() {
		return backChoice;
	}	
	public static void setBackChoice(int backChoice) {
		CourseActivity.backChoice = backChoice;
	}

	private boolean isExit = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_course);

		if (getFragmentManager().findFragmentById(R.id.main_show) == null) {
			getFragmentManager().beginTransaction().addToBackStack(null)
					.add(R.id.main_show2, new ResouceFragment()).commit();
		}

		// 返回
		findViewById(R.id.back).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				outback(getBackChoice());
			}
		});

		// 教学资料
		findViewById(R.id.button_data).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						findViewById(R.id.button_data).setBackgroundResource(
								R.drawable.button_data1);
						findViewById(R.id.button_discussion)
								.setBackgroundResource(
										R.drawable.button_discussion3);
						findViewById(R.id.button_notice).setBackgroundResource(
								R.drawable.button_notice3);
						getFragmentManager()
								.beginTransaction()
								.addToBackStack(null)
								.replace(R.id.main_show2,
										new ResouceFragment()).commit();

					}
				});

		// 课程讨论
		findViewById(R.id.button_discussion).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						findViewById(R.id.button_data).setBackgroundResource(
								R.drawable.button_data3);
						findViewById(R.id.button_discussion)
								.setBackgroundResource(
										R.drawable.button_discussion1);
						findViewById(R.id.button_notice).setBackgroundResource(
								R.drawable.button_notice3);
						Toast.makeText(getApplicationContext(), "课程讨论",
								Toast.LENGTH_SHORT).show();
					}
				});

		// 课程通知
		findViewById(R.id.button_notice).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						findViewById(R.id.button_data).setBackgroundResource(
								R.drawable.button_data3);// 杩樺師
						findViewById(R.id.button_discussion)
								.setBackgroundResource(
										R.drawable.button_discussion3);// 杩樺師
						findViewById(R.id.button_notice).setBackgroundResource(
								R.drawable.button_notice1);// 鎸変笅
						Toast.makeText(getApplicationContext(), "课程通知",
								Toast.LENGTH_SHORT).show();
					}
				});

		// 投影
//		findViewById(R.id.send_show).setOnTouchListener(new OnTouchListener() {
//			@SuppressLint("ClickableViewAccessibility")
//			@Override
//			public boolean onTouch(View v, MotionEvent event) {
//				if (event.getAction() == MotionEvent.ACTION_DOWN) {
//					// 保留有用，不要删
//				}
//				if (event.getAction() == MotionEvent.ACTION_UP) {
//					Toast.makeText(getApplicationContext(), "教学投影",
//							Toast.LENGTH_SHORT).show();
//				}
//				return false;
//			}
//		});
        findViewById(R.id.send_show).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                    new DeviceScanDialog(CourseActivity.this).getDialog().show();


            }
        });
		// 查询
		findViewById(R.id.search).setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					// 保留有用，不要删
				}
				if (event.getAction() == MotionEvent.ACTION_UP) {
					Toast.makeText(getApplicationContext(), "查询",
							Toast.LENGTH_SHORT).show();
				}
				return false;
			}
		});
		// 更多
		findViewById(R.id.more).setOnTouchListener(new OnTouchListener() {
			@SuppressLint("ClickableViewAccessibility")
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					// 保留有用，不要删
				}
				if (event.getAction() == MotionEvent.ACTION_UP) {
					Toast.makeText(getApplicationContext(), "更多",
							Toast.LENGTH_SHORT).show();
				}
				return false;
			}
		});

	}

	/*
	 * 锁定虚拟返回键 重写Activity中onKeyDown（）方法
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
//		finish();
//		Intent g = new Intent(CourseActivity.this, HomeActivity.class);
//		startActivity(g);
		outback(backChoice);
		return true;
	};



	private void outback(int i) {
		switch (i) {
		case 1: // 返回上层页面
			finish();
			Intent g = new Intent(CourseActivity.this, HomeActivity.class);
			startActivity(g);
			// android.os.Process.killProcess(android.os.Process.myPid());
			break;
		case 2:
			getFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.main_show2, new ResouceFragment()).commit();
			backChoice = 1;
			break;
		default:
			break;
		}

	}

}
